﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.JControls.JColorPicker;

namespace System.Windows.JControls
{
    /// <summary>
    /// Dialog zum auswählen einer Farbe
    /// </summary>
    public class JColorDialog : CommonDialog
    {
        public event EventHandler SelectedColorChanged;

        private Color _selectedColor;
        private string _text;

        public JColorDialog()
            : base()
        {
            _selectedColor = Color.Red;
            _text = "JColorDialog";
        }

        public Color SelectedColor
        {
            get { return _selectedColor; }
            set { _selectedColor = value; }
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public override void Reset()
        {
            _selectedColor = Color.Red;
        }

        protected override bool RunDialog(IntPtr hwndOwner)
        {
            FormColorDialogHandler frm = new FormColorDialogHandler();
            frm.ShowInTaskbar = false;
            frm.SelectedColorChanged += delegate(object sender, EventArgs e) { OnSelectedColorChanged(sender, e); };
            frm.Text = _text;

            frm.SelectedColor = _selectedColor;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                _selectedColor = frm.SelectedColor;
                return true;
            }

            return false;

        }

        protected virtual void OnSelectedColorChanged(object sender, EventArgs args)
        {
            if (SelectedColorChanged != null)
            {
                SelectedColorChanged(sender, args);
            }
        }
    }
}
